/*
 * PositioningSystem.h
 *
 *  Created on: Jan 1, 2011
 *      Author: Kevin Mueller
 */


#ifndef POSITIONINGSYSTEM_H
#define POSITIONINGSYSTEM_H

#include "../core/Opennav.h"

#include <QThread>
#include <QMutex>
#include <QGeoPositionInfoSource>

using namespace QtMobility;

class AbstractSetting;

typedef enum { SECOND, MILLISECOND } TimeUnit;


class ExtendedGeoPositionInfo : public QObject, public QtMobility::QGeoPositionInfo {
	Q_OBJECT

public:
	ExtendedGeoPositionInfo(const QtMobility::QGeoPositionInfo &current, QObject* parent=0);
	~ExtendedGeoPositionInfo();

	int getUnitSystem() const;
	qreal getVelocity() const;
	qreal getMovingDirection() const;
	qreal getDistance() const;
	QGeoCoordinate::CoordinateType getFixType() const;

	const opennav::RoadSegment* getRoadSegment() const;
	QString getRoadName() const;

private slots:
	void setUnitSystem(AbstractSetting *s);

private:
	void setUnitSystem(int s);

	int system;
};

class PositioningSystem : protected QThread{
	Q_OBJECT

	friend class ExtendedGeoPositionInfo;

public:
	PositioningSystem(QObject *parent=0);
	~PositioningSystem();

	void init();
	void enable();
	
	bool isEnabled();

	QtMobility::QGeoPositionInfo getLastPosition();

signals:
	void updated(const ExtendedGeoPositionInfo*);
	void startedMoving();
	void stoppedMoving();

public slots:
	void toggle();
	void setInterval(AbstractSetting *s);
	void setInterval(int value, TimeUnit unit=MILLISECOND);

	void normalMode();
	void powerSave();

private slots:
	void positionUpdated(const QGeoPositionInfo &info);

private:
	void disable();
	void run();

	bool isMoving;

	bool enabled;
	ExtendedGeoPositionInfo *m;
	QtMobility::QGeoPositionInfoSource *source;

	QMutex mutex;
	opennav::RoadSegment* currentSegment;
	bool threadRunning;
};



#endif
